/*******************************************************************************
 ** Name: protocol.cpp                                                        **
 ** Description:                                                              **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  Redistribution and use in source and binary forms, with or without       **
 **  modification, are permitted provided that the following conditions are   **
 **  met:                                                                     **
 **                                                                           **
 **  * Redistributions of source code must retain the above copyright notice, **
 **    this list of conditions and the following disclaimer.                  **
 **  * Redistributions in binary form must reproduce the above copyright      **
 **    notice, this list of conditions and the following disclaimer in the    **
 **    documentation and/or other materials provided with the distribution.   **
 **                                                                           **
 **  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      **
 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED **
 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           **
 ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT        **
 ** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    **
 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  **
 ** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR    **
 ** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    **
 ** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      **
 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        **
 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              **
 **                                                                           **
 ******************************************************************************/
// Created on April 4, 2010, 4:09 AM
// Modified $Date: 2011-11-24 00:07:02 -0700 (Thu, 24 Nov 2011) $ by $Author: unknown $
#define protocol_cpp_ID   "$Id: protocol.cpp 63608 2011-11-24 07:07:02Z unknown $"

#include <cstring>
#include <cstdlib>

#include "log.hpp"
#include "protocol.hpp"
#include "cmd.hpp"

static const char * WHITE_SPACE  = " \r\t\n";

// s = "put gun on table" etc
PktProtocol::PktProtocol(const char * s)
{
    char * buff = new char [strlen(s)+1];
    argv = new char * [MAX_CMD_ARGS+1];
    argc = 0;
    char * ptr, *state;

    strcpy(buff,s);

    if ( (ptr = std::strtok(buff,WHITE_SPACE)) == NULL)
        return;

    argv[argc] = ptr;


    while( (ptr = std::strtok(NULL,WHITE_SPACE)) != NULL)
    {
        if (++argc >= DEFAULT_MAX_ARGC) {
        	argv[argc] = NULL;
            return;
        }
        // skip articles and prepositions
        //if (Cmd::IsArticle(ptr) || Cmd::IsPrep(ptr))
         //   continue;
        argv[argc] = ptr;
    }
    argv[argc+1] = NULL;
    
//    cmdString = s;
//     cmdString = argv[0];
//     cmdString += L" ";
//     for (int x = 1; x <= argc; x++) {
//        cmdString += argv[x];
//        cmdString += L" ";
//    }

    delete [] buff;
}
